package com.example.sneakerappforme1105;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sneakerappforme1105.ViewModel.CartViewModel;
import com.example.sneakerappforme1105.adapter.OrderPagerAdapter;
import com.example.sneakerappforme1105.bean.CartItem;
import com.example.sneakerappforme1105.bean.Goods;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ShowOrderActivity extends AppCompatActivity implements DataProvider{
    private String TAG = "ShowOrderActivity";
    private CartViewModel cartViewModel;
    private HashMap<Long, Goods> mGoodsMap;
    private Long mUserId;
    private Long mCartId;
    private int idx; // 设置展示页，待付款、待发货、待收货
    private List<String> mTitleList = new ArrayList<>();
    private TextView tv_title;
    private ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_order);
        mTitleList.add("全部");
        mTitleList.add("待付款");
        mTitleList.add("待发货");
        mTitleList.add("待收货");
        iv_back = findViewById(R.id.iv_back);
        cartViewModel = MainApplication.getInstance().getCartViewModel();
        cartViewModel.getGoodsMap().observe(this, map->{
            mGoodsMap = map !=null ? map: new HashMap<>();  //从全局变量拿，不改变数据
        });
        // 注意回退需要传回去
        mUserId = getIntent().getLongExtra("userId",0L);
        mCartId = getIntent().getLongExtra("cartId",0L);
        Log.d(TAG,"用户id 为"+mUserId);
        Log.d(TAG, "购物车id 为"+mCartId);
        idx = getIntent().getIntExtra("idx",0);




        TabLayout tab_title = findViewById(R.id.tab_title);

        ViewPager2 vp2_content = findViewById(R.id.vp2_content);


        OrderPagerAdapter adapter = new OrderPagerAdapter(this, mTitleList);
        vp2_content.setAdapter(adapter); // 设置二代翻页视图的适配器


        new TabLayoutMediator(tab_title, vp2_content, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(mTitleList.get(position));  // 服装、电器
            }
        }).attach();

        // 根据 idx 设置初始选中页
        if (idx >= 0 && idx < mTitleList.size()) {
            vp2_content.setCurrentItem(idx, false); // false 表示禁用切换动画
        }

        tv_title = findViewById(R.id.tv_title3);
        tv_title.setText("订单");

        findViewById(R.id.iv_back3).setOnClickListener(view -> {
            Intent intent = new Intent(this, RootActivity.class);
            intent.putExtra("cartId",mCartId);
            intent.putExtra("userId",mUserId);
            intent.putExtra("MyFragment",2);
            startActivity(intent);
        });

    }

    @Override
    public Long getUserId() {
        return mUserId;
    }

    @Override
    public Long getCartId() {
        return null;
    }

    @Override
    public CartViewModel getCartViewModel() {
        return cartViewModel;
    }

    @Override
    public List<CartItem> getCartItemList() {
        return null;
    }

    @Override
    public HashMap<Long, Goods> getGoodsMap() {
        return mGoodsMap;
    }
}